package tchk.xyz.lingyuntutorminiprogram.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import tchk.xyz.lingyuntutorminiprogram.dto.DashboardStatsDTO;
import tchk.xyz.lingyuntutorminiprogram.dto.TeacherAuditDTO;
import tchk.xyz.lingyuntutorminiprogram.dto.TeacherDetailDTO;
import tchk.xyz.lingyuntutorminiprogram.dto.WithdrawalAuditDTO;
import tchk.xyz.lingyuntutorminiprogram.service.IOrdersService;
import tchk.xyz.lingyuntutorminiprogram.service.ITeacherInfoService;
import tchk.xyz.lingyuntutorminiprogram.service.IWalletTransactionService;
import tchk.xyz.lingyuntutorminiprogram.utils.Result;

@RestController
@RequestMapping("/api/admin")
public class AdminController {

    @Autowired
    private ITeacherInfoService teacherInfoService;
    @Autowired
    private IOrdersService ordersService;

    @Autowired
    private IWalletTransactionService walletTransactionService;

    /**
     * 获取待审核的教师列表（分页）
     */
    @GetMapping("/teachers/pending")
    @PreAuthorize("hasAuthority('admin:teacher:audit')") // 暂时注释掉，方便测试
    public Result<Object> getPendingTeachers(@RequestParam(defaultValue = "1") long current,
                                             @RequestParam(defaultValue = "10") long size) {
        Page<TeacherAuditDTO> page = new Page<>(current, size);
        return Result.success(teacherInfoService.getPendingTeachers(page));
    }

    /**
     * 审核教师资质
     */
    @PostMapping("/teachers/audit")
    @PreAuthorize("hasAuthority('admin:teacher:audit')")
    public Result<Void> auditTeacher(@RequestParam Long teacherUserId,
                                     @RequestParam Boolean isApproved,
                                     @RequestParam(required = false) String remark) {
        teacherInfoService.auditTeacher(teacherUserId, isApproved, remark);
        return Result.success();
    }

    @GetMapping("/dashboard/stats")
    @PreAuthorize("hasAuthority('admin:user:manage')")
    public Result<DashboardStatsDTO> getDashboardStats() {
        return Result.success(ordersService.getDashboardStats());
    }

    /**
     * 获取教师认证详情
     */
/*    @GetMapping("/teacher/detail/{userId}")
    @PreAuthorize("hasAuthority('admin:teacher:audit')")
    public Result<TeacherDetailDTO> getTeacherDetail(@PathVariable Long userId) {
        return Result.success(teacherInfoService.getTeacherDetailById(userId));
    }*/

    @GetMapping("/withdrawals") // 路径改为更通用的 /withdrawals
    @PreAuthorize("hasAuthority('admin:finance:audit')")
    public Result<IPage<WithdrawalAuditDTO>> getWithdrawals(@RequestParam(defaultValue = "1") long current,
                                                            @RequestParam(defaultValue = "10") long size,
                                                            @RequestParam(required = false) String status) { // 增加status参数
        Page<WithdrawalAuditDTO> page = new Page<>(current, size);
        return Result.success(walletTransactionService.getWithdrawalsByStatus(page, status));
    }

    @PostMapping("/withdrawals/audit")
    @PreAuthorize("hasAuthority('admin:finance:audit')")
    public Result<Void> auditWithdrawal(@RequestParam Long transactionId, @RequestParam Boolean isApproved) {
        walletTransactionService.auditWithdrawal(transactionId, isApproved);
        return Result.success();
    }

}